// Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MSGFRAGMENT_H
#define MSGFRAGMENT_H

#include "Communication/Socket/VmiSocket.h"
#include "Include/msghead.h"
#include "MemoryManagement/VmiBuffer.h"

class MsgFragment {
public:
    /**
     * @brief: constructor
     */
    MsgFragment();
    ~MsgFragment();

    /**
     * @brief: Network message fragmentation. Note that the parameter buf points to the stream_msg_head header.
     *         Datalen only contains the data part. When successful, the return value is the length of the data
     *         part, i.e. datalen. If the return value is less than 0, it fails.
     * @param [in] type: type
     * @param [in] socket: socket
     * @param [in] buf: data
     * @param [in] dataLen: data length
     * @return: dataLen or error code
     */
    int FragmentSend(VMIMsgType type, std::shared_ptr<VmiSocket> socket, StreamMsgHead& buf, int dataLen);

    /*
     * @brief: Set whether to send the whole package without going through the fragmentation process.
     * @param [in] flag：True means that the whole packet is sent regardless of the data.
     */
    void SetSingleSend(bool flag);

private:

    /**
     * @brief: init message what to be send
     * @param [in] messageHead: message
     * @prama [in] type: type
     * @param [in] dataLen: data length
     */
    void InitSendMsg(StreamMsgHead& messageHead, uint8_t flag, VMIMsgType type, uint32_t dataLen);
    bool m_singleSend = false;
};

#endif
